unit frm_ResultGrid;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, shbl_Card, PropFilerEh, MemTableDataEh, Db, DBGridEhGrouping,
  GridsEh, DBGridEh, MemTableEh, PropStorageEh, ActnList, ComCtrls,
  ToolWin, StdCtrls, Buttons, ExtCtrls, XLSExportComp, pFIBDataSet;

type

  TOnDblClick = procedure (Sender: TObject) of object; // function(Index: Longint): Integer of object;

  TfrmResultGrid = class(TshblCard)
    mteList: TMemTableEh;
    dscList: TDataSource;
    dbgList: TDBGridEh;
    XLSExportGrid: TXLSExportDBGridEh;
    XLSExportFile: TXLSExportFile;
    SaveXLSDialog: TSaveDialog;
    procedure aCancelExecute(Sender: TObject);
    procedure aSaveExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

procedure ShowResultGrid(DataSet :TDataSet; InvisibleFields :array of string; OnDblClick :TOnDblClick; CloseDataSetAfterOpen :boolean; aOwner :TComponent);

implementation

uses uMAIN;

{$R *.dfm}

procedure ShowResultGrid(DataSet :TDataSet; InvisibleFields :array of string; OnDblClick :TOnDblClick; CloseDataSetAfterOpen :boolean; aOwner :TComponent);
var
  frmResultGrid: TfrmResultGrid;
  field_cnt :integer;
begin
  frmResultGrid := TfrmResultGrid.Create(aOwner);

  if Assigned(OnDblClick) then
    frmResultGrid.dbgList.OnDblClick := OnDblClick;

  frmResultGrid.mteList.LoadFromDataSet(DataSet, -1, lmCopy, false);

  field_cnt := Length(InvisibleFields);

  while field_cnt > 0 do begin
    frmResultGrid.dbgList.FindFieldColumn(InvisibleFields[field_cnt - 1]).Visible := false;
    Dec(field_cnt);
  end;

  if CloseDataSetAfterOpen then
    (DataSet as TpFIBDataSet).Transaction.Rollback;

  frmResultGrid.ShowModal;

  FreeAndNil(frmResultGrid);
end;


procedure TfrmResultGrid.aCancelExecute(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TfrmResultGrid.aSaveExecute(Sender: TObject);
begin
  if mteList.RecordCount > 0 then begin
    if SaveXLSDialog.Execute then begin

      XLSExportFile.Clear;
      XLSExportGrid.ExportData(0, 0, 0);

      XLSExportFile.SaveToFile(SaveXLSDialog.FileName);
    end;
  end;
end;

end.
